package main

import (
	"encoding/json"
	"fmt"
)

//自定义类型
type myInt int

//类型别名
type newInt = int

//todo go语言基础之结构体 第一个视频完

func main() {

	//var i myInt
	//fmt.Printf("%T %v", i, i)

	//var user User
	//var user1 = new(User)
	//
	//user1.id = 1
	//user1.name = "carrymec"
	//user1.age = 22
	//fmt.Printf("p2=%#v\n",user1)

	//m := make(map[string]*student)
	//stus := []student{
	//	{name: "小王子", age: 18},
	//	{name: "娜扎", age: 23},
	//	{name: "大王八", age: 9000},
	//}
	//for _, stu := range stus {
	//	m[stu.name] = &stu
	//}
	//for k, v := range m {
	//	fmt.Println(k, "=>", v.name)
	//}
	user := User{
		name: "carrymec",
		id:   1,
		age:  22,
		Address: Address{
			Province: "广东",
			City:     "深圳",
		},
	}

	fmt.Printf("user = %#v \n", user)

	u := user1("aa", 11, 11)
	u.Dream()

	s1 := Student{
		ID:     1,
		Gender: "男",
		name:   "沙河娜扎",
	}
	data, err := json.Marshal(s1)
	if err != nil {
		fmt.Println("json marshal failed!")
		return
	}
	fmt.Printf("json str:%s\n", data) //json str:{"id":1,"Gender":"男"}

}

//结构体

//Student 学生
type Student struct {
	ID     int    `json:"id"` //通过指定tag实现json序列化该字段时的key
	Gender string //json序列化是默认使用字段名作为key
	name   string `json:"name"` //私有不能被json包访问
}

type Address struct {
	Province string
	City     string
}
type User struct {
	id   int
	name string
	age  int
	Address
}

func user1(name string, age int, id int) *User {
	return &User{
		id:   id,
		name: name,
		age:  age,
	}
}

func (p User) Dream() {
	fmt.Printf("%s的梦想是学好Go语言！\n", p.name)
}
